/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.opensymphony.workflow.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jackysoft.util.ValidationUtils;


/**
 * 
 */


public class RestrictionDescriptor extends AbstractDescriptor implements Validatable {
    //~ Instance fields ////////////////////////////////////////////////////////

    /**
	 * 
	 */
	private static final long serialVersionUID = -5333495419541613819L;
	protected List conditions = new ArrayList();

    //~ Constructors ///////////////////////////////////////////////////////////

    public RestrictionDescriptor() {
    }

    public RestrictionDescriptor(org.dom4j.Element element) {
    	super(element);
    	this.init(element);
    }

    //~ Methods ////////////////////////////////////////////////////////////////

    
    
    
    
    
    
    public List getConditions() {
        return conditions;
    }

    public void setConditionsDescriptor(ConditionsDescriptor descriptor) {
        if (conditions.size() == 1) {
            conditions.set(0, descriptor);
        } else {
            conditions.add(descriptor);
        }
    }

    
    public ConditionsDescriptor getConditionsDescriptor() {
        if (conditions.size() == 0) {
            return null;
        }

        return (ConditionsDescriptor) conditions.get(0);
    }

    public void validate() throws InvalidWorkflowDescriptorException {
        if (conditions.size() > 1) {
            throw new InvalidWorkflowDescriptorException("A restrict-to element can only have one conditions child element");
        }

        ValidationUtils.validate(conditions);
    }

    protected void init(org.dom4j.Element restriction) {
    	this.conditions = new ArrayList();
    	Iterator conditionsItr = restriction.elementIterator("conditions");
    	for(;conditionsItr.hasNext();){
    		org.dom4j.Element conditionsElement = (org.dom4j.Element) conditionsItr.next();
    		ConditionsDescriptor cds = new ConditionsDescriptor(conditionsElement);
    		this.conditions.add(cds);
    	}
    	//buildElement(restriction, "conditions", "condition", ConditionsDescriptor.class, this.conditions, this);    	
    }
	/**
	 * 
	 */
	public void setConditions(List conditions) {
		this.conditions = conditions;
	}
    
    
}
